<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>pgbench</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REV="MADE"
HREF="mailto:pgsql-docs@postgresql.org"><LINK
REL="HOME"
TITLE="PostgreSQL 9.1.2 Documentation"
HREF="index.html"><LINK
REL="UP"
TITLE="Additional Supplied Modules"
HREF="contrib.html"><LINK
REL="PREVIOUS"
TITLE="pg_archivecleanup"
HREF="pgarchivecleanup.html"><LINK
REL="NEXT"
TITLE="pg_buffercache"
HREF="pgbuffercache.html"><LINK
REL="STYLESHEET"
TYPE="text/css"
HREF="stylesheet.css"><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=ISO-8859-1"><META
NAME="creation"
CONTENT="2011-12-01T22:07:59"></HEAD
><BODY
CLASS="SECT1"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="5"
ALIGN="center"
VALIGN="bottom"
><A
HREF="index.html"
>PostgreSQL 9.1.2 Documentation</A
></TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
TITLE="pg_archivecleanup"
HREF="pgarchivecleanup.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="contrib.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="60%"
ALIGN="center"
VALIGN="bottom"
>Appendix F. Additional Supplied Modules</TD
><TD
WIDTH="20%"
ALIGN="right"
VALIGN="top"
><A
TITLE="pg_buffercache"
HREF="pgbuffercache.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="PGBENCH"
>F.26. pgbench</A
></H1
><P
>  <SPAN
CLASS="APPLICATION"
>pgbench</SPAN
> is a simple program for running benchmark
  tests on <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>.  It runs the same sequence of SQL
  commands over and over, possibly in multiple concurrent database sessions,
  and then calculates the average transaction rate (transactions per second).
  By default, <SPAN
CLASS="APPLICATION"
>pgbench</SPAN
> tests a scenario that is
  loosely based on TPC-B, involving five <TT
CLASS="COMMAND"
>SELECT</TT
>,
  <TT
CLASS="COMMAND"
>UPDATE</TT
>, and <TT
CLASS="COMMAND"
>INSERT</TT
> commands per transaction.
  However, it is easy to test other cases by writing your own transaction
  script files.
 </P
><P
>  Typical output from pgbench looks like:

</P><PRE
CLASS="SCREEN"
>transaction type: TPC-B (sort of)
scaling factor: 10
query mode: simple
number of clients: 10
number of threads: 1
number of transactions per client: 1000
number of transactions actually processed: 10000/10000
tps = 85.184871 (including connections establishing)
tps = 85.296346 (excluding connections establishing)</PRE
><P>

  The first six lines report some of the most important parameter
  settings.  The next line reports the number of transactions completed
  and intended (the latter being just the product of number of clients
  and number of transactions per client); these will be equal unless the run
  failed before completion.  (In <TT
CLASS="LITERAL"
>-T</TT
> mode, only the actual
  number of transactions is printed.)
  The last two lines report the number of transactions per second,
  figured with and without counting the time to start database sessions.
 </P
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN133905"
>F.26.1. Overview</A
></H2
><P
>   The default TPC-B-like transaction test requires specific tables to be
   set up beforehand.  <SPAN
CLASS="APPLICATION"
>pgbench</SPAN
> should be invoked with
   the <TT
CLASS="LITERAL"
>-i</TT
> (initialize) option to create and populate these
   tables.  (When you are testing a custom script, you don't need this
   step, but will instead need to do whatever setup your test needs.)
   Initialization looks like:

</P><PRE
CLASS="PROGRAMLISTING"
>pgbench -i [<SPAN
CLASS="OPTIONAL"
> <TT
CLASS="REPLACEABLE"
><I
>other-options</I
></TT
> </SPAN
>] <TT
CLASS="REPLACEABLE"
><I
>dbname</I
></TT
></PRE
><P>

   where <TT
CLASS="REPLACEABLE"
><I
>dbname</I
></TT
> is the name of the already-created
   database to test in.  (You may also need <TT
CLASS="LITERAL"
>-h</TT
>,
   <TT
CLASS="LITERAL"
>-p</TT
>, and/or <TT
CLASS="LITERAL"
>-U</TT
> options to specify how to
   connect to the database server.)
  </P
><DIV
CLASS="CAUTION"
><P
></P
><TABLE
CLASS="CAUTION"
BORDER="1"
WIDTH="100%"
><TR
><TD
ALIGN="CENTER"
><B
>Caution</B
></TD
></TR
><TR
><TD
ALIGN="LEFT"
><P
>    <TT
CLASS="LITERAL"
>pgbench -i</TT
> creates four tables <TT
CLASS="STRUCTNAME"
>pgbench_accounts</TT
>,
    <TT
CLASS="STRUCTNAME"
>pgbench_branches</TT
>, <TT
CLASS="STRUCTNAME"
>pgbench_history</TT
>, and
    <TT
CLASS="STRUCTNAME"
>pgbench_tellers</TT
>,
    destroying any existing tables of these names.
    Be very careful to use another database if you have tables having these
    names!
   </P
></TD
></TR
></TABLE
></DIV
><P
>   At the default <SPAN
CLASS="QUOTE"
>"scale factor"</SPAN
> of 1, the tables initially
   contain this many rows:
</P><PRE
CLASS="SCREEN"
>table                   # of rows
---------------------------------
pgbench_branches        1
pgbench_tellers         10
pgbench_accounts        100000
pgbench_history         0</PRE
><P>
   You can (and, for most purposes, probably should) increase the number
   of rows by using the <TT
CLASS="LITERAL"
>-s</TT
> (scale factor) option.  The
   <TT
CLASS="LITERAL"
>-F</TT
> (fillfactor) option might also be used at this point.
  </P
><P
>   Once you have done the necessary setup, you can run your benchmark
   with a command that doesn't include <TT
CLASS="LITERAL"
>-i</TT
>, that is

</P><PRE
CLASS="PROGRAMLISTING"
>pgbench [<SPAN
CLASS="OPTIONAL"
> <TT
CLASS="REPLACEABLE"
><I
>options</I
></TT
> </SPAN
>] <TT
CLASS="REPLACEABLE"
><I
>dbname</I
></TT
></PRE
><P>

   In nearly all cases, you'll need some options to make a useful test.
   The most important options are <TT
CLASS="LITERAL"
>-c</TT
> (number of clients),
   <TT
CLASS="LITERAL"
>-t</TT
> (number of transactions), <TT
CLASS="LITERAL"
>-T</TT
> (time limit),
   and <TT
CLASS="LITERAL"
>-f</TT
> (specify a custom script file).
   See below for a full list.
  </P
><P
>   <A
HREF="pgbench.html#PGBENCH-INIT-OPTIONS"
>Section F.26.2</A
> shows options that are used
   during database initialization, while
   <A
HREF="pgbench.html#PGBENCH-RUN-OPTIONS"
>Section F.26.3</A
> shows options that are used
   while running benchmarks, and
   <A
HREF="pgbench.html#PGBENCH-COMMON-OPTIONS"
>Section F.26.4</A
> shows options that are useful
   in both cases.
  </P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="PGBENCH-INIT-OPTIONS"
>F.26.2. <SPAN
CLASS="APPLICATION"
>pgbench</SPAN
> Initialization Options</A
></H2
><P
>    <SPAN
CLASS="APPLICATION"
>pgbench</SPAN
> accepts the following command-line
    initialization arguments:

    <P
></P
></P><DIV
CLASS="VARIABLELIST"
><DL
><DT
><TT
CLASS="OPTION"
>-i</TT
></DT
><DD
><P
>        Required to invoke initialization mode.
       </P
></DD
><DT
><TT
CLASS="OPTION"
>-F</TT
> <TT
CLASS="REPLACEABLE"
><I
>fillfactor</I
></TT
></DT
><DD
><P
>        Create the <TT
CLASS="STRUCTNAME"
>pgbench_accounts</TT
>,
        <TT
CLASS="STRUCTNAME"
>pgbench_tellers</TT
> and
        <TT
CLASS="STRUCTNAME"
>pgbench_branches</TT
> tables with the given fillfactor.
        Default is 100.
       </P
></DD
><DT
><TT
CLASS="OPTION"
>-s</TT
> <TT
CLASS="REPLACEABLE"
><I
>scale_factor</I
></TT
></DT
><DD
><P
>        Multiply the number of rows generated by the scale factor.
        For example, <TT
CLASS="LITERAL"
>-s 100</TT
> will create 10,000,000 rows
        in the <TT
CLASS="STRUCTNAME"
>pgbench_accounts</TT
> table. Default is 1.
       </P
></DD
></DL
></DIV
><P>
   </P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="PGBENCH-RUN-OPTIONS"
>F.26.3. <SPAN
CLASS="APPLICATION"
>pgbench</SPAN
> Benchmarking Options</A
></H2
><P
>    <SPAN
CLASS="APPLICATION"
>pgbench</SPAN
> accepts the following command-line
    benchmarking arguments:

    <P
></P
></P><DIV
CLASS="VARIABLELIST"
><DL
><DT
><TT
CLASS="OPTION"
>-c</TT
> <TT
CLASS="REPLACEABLE"
><I
>clients</I
></TT
></DT
><DD
><P
>        Number of clients simulated, that is, number of concurrent database
        sessions.  Default is 1.
       </P
></DD
><DT
><TT
CLASS="OPTION"
>-C</TT
></DT
><DD
><P
>        Establish a new connection for each transaction, rather than
        doing it just once per client session.
        This is useful to measure the connection overhead.
       </P
></DD
><DT
><TT
CLASS="OPTION"
>-d</TT
></DT
><DD
><P
>        Print debugging output.
       </P
></DD
><DT
><TT
CLASS="OPTION"
>-D</TT
> <TT
CLASS="REPLACEABLE"
><I
>varname</I
></TT
><TT
CLASS="LITERAL"
>=</TT
><TT
CLASS="REPLACEABLE"
><I
>value</I
></TT
></DT
><DD
><P
>        Define a variable for use by a custom script (see below).
        Multiple <TT
CLASS="LITERAL"
>-D</TT
> options are allowed.
       </P
></DD
><DT
><TT
CLASS="OPTION"
>-f</TT
> <TT
CLASS="REPLACEABLE"
><I
>filename</I
></TT
></DT
><DD
><P
>        Read transaction script from <TT
CLASS="REPLACEABLE"
><I
>filename</I
></TT
>.
        See below for details.
        <TT
CLASS="LITERAL"
>-N</TT
>, <TT
CLASS="LITERAL"
>-S</TT
>, and <TT
CLASS="LITERAL"
>-f</TT
>
        are mutually exclusive.
       </P
></DD
><DT
><TT
CLASS="OPTION"
>-j</TT
> <TT
CLASS="REPLACEABLE"
><I
>threads</I
></TT
></DT
><DD
><P
>        Number of worker threads within <SPAN
CLASS="APPLICATION"
>pgbench</SPAN
>.
        Using more than one thread can be helpful on multi-CPU machines.
        The number of clients must be a multiple of the number of threads,
        since each thread is given the same number of client sessions to manage.
        Default is 1.
       </P
></DD
><DT
><TT
CLASS="OPTION"
>-l</TT
></DT
><DD
><P
>        Write the time taken by each transaction to a log file.
        See below for details.
       </P
></DD
><DT
><TT
CLASS="OPTION"
>-M</TT
> <TT
CLASS="REPLACEABLE"
><I
>querymode</I
></TT
></DT
><DD
><P
>        Protocol to use for submitting queries to the server:
          <P
></P
></P><UL
><LI
><P
><TT
CLASS="LITERAL"
>simple</TT
>: use simple query protocol.</P
></LI
><LI
><P
><TT
CLASS="LITERAL"
>extended</TT
>: use extended query protocol.</P
></LI
><LI
><P
><TT
CLASS="LITERAL"
>prepared</TT
>: use extended query protocol with prepared statements.</P
></LI
></UL
><P>
        The default is simple query protocol.  (See <A
HREF="protocol.html"
>Chapter 46</A
>
        for more information.)
       </P
></DD
><DT
><TT
CLASS="OPTION"
>-n</TT
></DT
><DD
><P
>        Perform no vacuuming before running the test.
        This option is <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>necessary</I
></SPAN
>
        if you are running a custom test scenario that does not include
        the standard tables <TT
CLASS="STRUCTNAME"
>pgbench_accounts</TT
>,
        <TT
CLASS="STRUCTNAME"
>pgbench_branches</TT
>, <TT
CLASS="STRUCTNAME"
>pgbench_history</TT
>, and
        <TT
CLASS="STRUCTNAME"
>pgbench_tellers</TT
>.
       </P
></DD
><DT
><TT
CLASS="OPTION"
>-N</TT
></DT
><DD
><P
>        Do not update <TT
CLASS="STRUCTNAME"
>pgbench_tellers</TT
> and
        <TT
CLASS="STRUCTNAME"
>pgbench_branches</TT
>.
        This will avoid update contention on these tables, but
        it makes the test case even less like TPC-B.
       </P
></DD
><DT
><TT
CLASS="OPTION"
>-r</TT
></DT
><DD
><P
>        Report the average per-statement latency (execution time from the
        perspective of the client) of each command after the benchmark
        finishes.  See below for details.
       </P
></DD
><DT
><TT
CLASS="OPTION"
>-s</TT
> <TT
CLASS="REPLACEABLE"
><I
>scale_factor</I
></TT
></DT
><DD
><P
>        Report the specified scale factor in <SPAN
CLASS="APPLICATION"
>pgbench</SPAN
>'s
        output.  With the built-in tests, this is not necessary; the
        correct scale factor will be detected by counting the number of
        rows in the <TT
CLASS="STRUCTNAME"
>pgbench_branches</TT
> table.  However, when testing
        custom benchmarks (<TT
CLASS="LITERAL"
>-f</TT
> option), the scale factor
        will be reported as 1 unless this option is used.
       </P
></DD
><DT
><TT
CLASS="OPTION"
>-S</TT
></DT
><DD
><P
>        Perform select-only transactions instead of TPC-B-like test.
       </P
></DD
><DT
><TT
CLASS="OPTION"
>-t</TT
> <TT
CLASS="REPLACEABLE"
><I
>transactions</I
></TT
></DT
><DD
><P
>        Number of transactions each client runs.  Default is 10.
       </P
></DD
><DT
><TT
CLASS="OPTION"
>-T</TT
> <TT
CLASS="REPLACEABLE"
><I
>seconds</I
></TT
></DT
><DD
><P
>        Run the test for this many seconds, rather than a fixed number of
        transactions per client. <TT
CLASS="LITERAL"
>-t</TT
> and
        <TT
CLASS="LITERAL"
>-T</TT
> are mutually exclusive.
       </P
></DD
><DT
><TT
CLASS="OPTION"
>-v</TT
></DT
><DD
><P
>        Vacuum all four standard tables before running the test.
        With neither <TT
CLASS="LITERAL"
>-n</TT
> nor <TT
CLASS="LITERAL"
>-v</TT
>, pgbench will vacuum the
        <TT
CLASS="STRUCTNAME"
>pgbench_tellers</TT
> and <TT
CLASS="STRUCTNAME"
>pgbench_branches</TT
>
        tables, and will truncate <TT
CLASS="STRUCTNAME"
>pgbench_history</TT
>.
       </P
></DD
></DL
></DIV
><P>
   </P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="PGBENCH-COMMON-OPTIONS"
>F.26.4. <SPAN
CLASS="APPLICATION"
>pgbench</SPAN
> Common Options</A
></H2
><P
>    <SPAN
CLASS="APPLICATION"
>pgbench</SPAN
> accepts the following command-line
    common arguments:

    <P
></P
></P><DIV
CLASS="VARIABLELIST"
><DL
><DT
><TT
CLASS="OPTION"
>-h</TT
> <TT
CLASS="REPLACEABLE"
><I
>hostname</I
></TT
></DT
><DD
><P
>        The database server's host name
       </P
></DD
><DT
><TT
CLASS="OPTION"
>-p</TT
> <TT
CLASS="REPLACEABLE"
><I
>port</I
></TT
></DT
><DD
><P
>        The database server's port number
       </P
></DD
><DT
><TT
CLASS="OPTION"
>-U</TT
> <TT
CLASS="REPLACEABLE"
><I
>login</I
></TT
></DT
><DD
><P
>        The user name to connect as
       </P
></DD
></DL
></DIV
><P>
   </P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN134126"
>F.26.5. What is the <SPAN
CLASS="QUOTE"
>"Transaction"</SPAN
> Actually Performed in pgbench?</A
></H2
><P
>   The default transaction script issues seven commands per transaction:
  </P
><P
></P
><OL
TYPE="1"
><LI
><P
><TT
CLASS="LITERAL"
>BEGIN;</TT
></P
></LI
><LI
><P
><TT
CLASS="LITERAL"
>UPDATE pgbench_accounts SET abalance = abalance + :delta WHERE aid = :aid;</TT
></P
></LI
><LI
><P
><TT
CLASS="LITERAL"
>SELECT abalance FROM pgbench_accounts WHERE aid = :aid;</TT
></P
></LI
><LI
><P
><TT
CLASS="LITERAL"
>UPDATE pgbench_tellers SET tbalance = tbalance + :delta WHERE tid = :tid;</TT
></P
></LI
><LI
><P
><TT
CLASS="LITERAL"
>UPDATE pgbench_branches SET bbalance = bbalance + :delta WHERE bid = :bid;</TT
></P
></LI
><LI
><P
><TT
CLASS="LITERAL"
>INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP);</TT
></P
></LI
><LI
><P
><TT
CLASS="LITERAL"
>END;</TT
></P
></LI
></OL
><P
>   If you specify <TT
CLASS="LITERAL"
>-N</TT
>, steps 4 and 5 aren't included in the
   transaction.  If you specify <TT
CLASS="LITERAL"
>-S</TT
>, only the <TT
CLASS="COMMAND"
>SELECT</TT
> is
   issued.
  </P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN134156"
>F.26.6. Custom Scripts</A
></H2
><P
>   <SPAN
CLASS="APPLICATION"
>pgbench</SPAN
> has support for running custom
   benchmark scenarios by replacing the default transaction script
   (described above) with a transaction script read from a file
   (<TT
CLASS="LITERAL"
>-f</TT
> option).  In this case a <SPAN
CLASS="QUOTE"
>"transaction"</SPAN
>
   counts as one execution of a script file.  You can even specify
   multiple scripts (multiple <TT
CLASS="LITERAL"
>-f</TT
> options), in which
   case a random one of the scripts is chosen each time a client session
   starts a new transaction.
  </P
><P
>   The format of a script file is one SQL command per line; multiline
   SQL commands are not supported.  Empty lines and lines beginning with
   <TT
CLASS="LITERAL"
>--</TT
> are ignored.  Script file lines can also be
   <SPAN
CLASS="QUOTE"
>"meta commands"</SPAN
>, which are interpreted by <SPAN
CLASS="APPLICATION"
>pgbench</SPAN
>
   itself, as described below.
  </P
><P
>   There is a simple variable-substitution facility for script files.
   Variables can be set by the command-line <TT
CLASS="LITERAL"
>-D</TT
> option,
   explained above, or by the meta commands explained below.
   In addition to any variables preset by <TT
CLASS="LITERAL"
>-D</TT
> command-line options,
   the variable <TT
CLASS="LITERAL"
>scale</TT
> is preset to the current scale factor.
   Once set, a variable's
   value can be inserted into a SQL command by writing
   <TT
CLASS="LITERAL"
>:</TT
><TT
CLASS="REPLACEABLE"
><I
>variablename</I
></TT
>.  When running more than
   one client session, each session has its own set of variables.
  </P
><P
>   Script file meta commands begin with a backslash (<TT
CLASS="LITERAL"
>\</TT
>).
   Arguments to a meta command are separated by white space.
   These meta commands are supported:
  </P
><P
></P
><DIV
CLASS="VARIABLELIST"
><DL
><DT
><TT
CLASS="LITERAL"
>\set <TT
CLASS="REPLACEABLE"
><I
>varname</I
></TT
> <TT
CLASS="REPLACEABLE"
><I
>operand1</I
></TT
> [ <TT
CLASS="REPLACEABLE"
><I
>operator</I
></TT
> <TT
CLASS="REPLACEABLE"
><I
>operand2</I
></TT
> ]</TT
></DT
><DD
><P
>      Sets variable <TT
CLASS="REPLACEABLE"
><I
>varname</I
></TT
> to a calculated integer value.
      Each <TT
CLASS="REPLACEABLE"
><I
>operand</I
></TT
> is either an integer constant or a
      <TT
CLASS="LITERAL"
>:</TT
><TT
CLASS="REPLACEABLE"
><I
>variablename</I
></TT
> reference to a variable
      having an integer value.  The <TT
CLASS="REPLACEABLE"
><I
>operator</I
></TT
> can be
      <TT
CLASS="LITERAL"
>+</TT
>, <TT
CLASS="LITERAL"
>-</TT
>, <TT
CLASS="LITERAL"
>*</TT
>, or <TT
CLASS="LITERAL"
>/</TT
>.
     </P
><P
>      Example:
</P><PRE
CLASS="PROGRAMLISTING"
>\set ntellers 10 * :scale</PRE
><P>
     </P
></DD
><DT
><TT
CLASS="LITERAL"
>\setrandom <TT
CLASS="REPLACEABLE"
><I
>varname</I
></TT
> <TT
CLASS="REPLACEABLE"
><I
>min</I
></TT
> <TT
CLASS="REPLACEABLE"
><I
>max</I
></TT
></TT
></DT
><DD
><P
>      Sets variable <TT
CLASS="REPLACEABLE"
><I
>varname</I
></TT
> to a random integer value
      between the limits <TT
CLASS="REPLACEABLE"
><I
>min</I
></TT
> and <TT
CLASS="REPLACEABLE"
><I
>max</I
></TT
> inclusive.
      Each limit can be either an integer constant or a
      <TT
CLASS="LITERAL"
>:</TT
><TT
CLASS="REPLACEABLE"
><I
>variablename</I
></TT
> reference to a variable
      having an integer value.
     </P
><P
>      Example:
</P><PRE
CLASS="PROGRAMLISTING"
>\setrandom aid 1 :naccounts</PRE
><P>
     </P
></DD
><DT
><TT
CLASS="LITERAL"
>\sleep <TT
CLASS="REPLACEABLE"
><I
>number</I
></TT
> [ us | ms | s ]</TT
></DT
><DD
><P
>      Causes script execution to sleep for the specified duration in
      microseconds (<TT
CLASS="LITERAL"
>us</TT
>), milliseconds (<TT
CLASS="LITERAL"
>ms</TT
>) or seconds
      (<TT
CLASS="LITERAL"
>s</TT
>).  If the unit is omitted then seconds are the default.
      <TT
CLASS="REPLACEABLE"
><I
>number</I
></TT
> can be either an integer constant or a
      <TT
CLASS="LITERAL"
>:</TT
><TT
CLASS="REPLACEABLE"
><I
>variablename</I
></TT
> reference to a variable
      having an integer value.
     </P
><P
>      Example:
</P><PRE
CLASS="PROGRAMLISTING"
>\sleep 10 ms</PRE
><P>
     </P
></DD
><DT
><TT
CLASS="LITERAL"
>\setshell <TT
CLASS="REPLACEABLE"
><I
>varname</I
></TT
> <TT
CLASS="REPLACEABLE"
><I
>command</I
></TT
> [ <TT
CLASS="REPLACEABLE"
><I
>argument</I
></TT
> ... ]</TT
></DT
><DD
><P
>      Sets variable <TT
CLASS="REPLACEABLE"
><I
>varname</I
></TT
> to the result of the shell command
      <TT
CLASS="REPLACEABLE"
><I
>command</I
></TT
>. The command must return an integer value
      through its standard output.
     </P
><P
>      <TT
CLASS="REPLACEABLE"
><I
>argument</I
></TT
> can be either a text constant or a
      <TT
CLASS="LITERAL"
>:</TT
><TT
CLASS="REPLACEABLE"
><I
>variablename</I
></TT
> reference to a variable of
      any types. If you want to use <TT
CLASS="REPLACEABLE"
><I
>argument</I
></TT
> starting with
      colons, you need to add an additional colon at the beginning of
      <TT
CLASS="REPLACEABLE"
><I
>argument</I
></TT
>.
     </P
><P
>      Example:
</P><PRE
CLASS="PROGRAMLISTING"
>\setshell variable_to_be_assigned command literal_argument :variable ::literal_starting_with_colon</PRE
><P>
     </P
></DD
><DT
><TT
CLASS="LITERAL"
>\shell <TT
CLASS="REPLACEABLE"
><I
>command</I
></TT
> [ <TT
CLASS="REPLACEABLE"
><I
>argument</I
></TT
> ... ]</TT
></DT
><DD
><P
>      Same as <TT
CLASS="LITERAL"
>\setshell</TT
>, but the result is ignored.
     </P
><P
>      Example:
</P><PRE
CLASS="PROGRAMLISTING"
>\shell command literal_argument :variable ::literal_starting_with_colon</PRE
><P>
     </P
></DD
></DL
></DIV
><P
>   As an example, the full definition of the built-in TPC-B-like
   transaction is:

</P><PRE
CLASS="PROGRAMLISTING"
>\set nbranches :scale
\set ntellers 10 * :scale
\set naccounts 100000 * :scale
\setrandom aid 1 :naccounts
\setrandom bid 1 :nbranches
\setrandom tid 1 :ntellers
\setrandom delta -5000 5000
BEGIN;
UPDATE pgbench_accounts SET abalance = abalance + :delta WHERE aid = :aid;
SELECT abalance FROM pgbench_accounts WHERE aid = :aid;
UPDATE pgbench_tellers SET tbalance = tbalance + :delta WHERE tid = :tid;
UPDATE pgbench_branches SET bbalance = bbalance + :delta WHERE bid = :bid;
INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP);
END;</PRE
><P>

   This script allows each iteration of the transaction to reference
   different, randomly-chosen rows.  (This example also shows why it's
   important for each client session to have its own variables &mdash;
   otherwise they'd not be independently touching different rows.)
  </P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN134255"
>F.26.7. Per-Transaction Logging</A
></H2
><P
>   With the <TT
CLASS="LITERAL"
>-l</TT
> option, <SPAN
CLASS="APPLICATION"
>pgbench</SPAN
> writes the time
   taken by each transaction to a log file.  The log file will be named
   <TT
CLASS="FILENAME"
>pgbench_log.<TT
CLASS="REPLACEABLE"
><I
>nnn</I
></TT
></TT
>, where
   <TT
CLASS="REPLACEABLE"
><I
>nnn</I
></TT
> is the PID of the pgbench process.
   If the <TT
CLASS="LITERAL"
>-j</TT
> option is 2 or higher, creating multiple worker
   threads, each will have its own log file. The first worker will use the
   same name for its log file as in the standard single worker case.
   The additional log files for the other workers will be named
   <TT
CLASS="FILENAME"
>pgbench_log.<TT
CLASS="REPLACEABLE"
><I
>nnn</I
></TT
>.<TT
CLASS="REPLACEABLE"
><I
>mmm</I
></TT
></TT
>,
   where <TT
CLASS="REPLACEABLE"
><I
>mmm</I
></TT
> is a sequential number for each worker starting
   with 1.
  </P
><P
>   The format of the log is:

</P><PRE
CLASS="SYNOPSIS"
><TT
CLASS="REPLACEABLE"
><I
>client_id</I
></TT
> <TT
CLASS="REPLACEABLE"
><I
>transaction_no</I
></TT
> <TT
CLASS="REPLACEABLE"
><I
>time</I
></TT
> <TT
CLASS="REPLACEABLE"
><I
>file_no</I
></TT
> <TT
CLASS="REPLACEABLE"
><I
>time_epoch</I
></TT
> <TT
CLASS="REPLACEABLE"
><I
>time_us</I
></TT
></PRE
><P>

   where <TT
CLASS="REPLACEABLE"
><I
>time</I
></TT
> is the total elapsed transaction time in microseconds,
   <TT
CLASS="REPLACEABLE"
><I
>file_no</I
></TT
> identifies which script file was used
   (useful when multiple scripts were specified with <TT
CLASS="LITERAL"
>-f</TT
>),
   and <TT
CLASS="REPLACEABLE"
><I
>time_epoch</I
></TT
>/<TT
CLASS="REPLACEABLE"
><I
>time_us</I
></TT
> are a
   UNIX epoch format timestamp and an offset
   in microseconds (suitable for creating a ISO 8601
   timestamp with fractional seconds) showing when
   the transaction completed.
  </P
><P
>   Here are example outputs:
</P><PRE
CLASS="SCREEN"
> 0 199 2241 0 1175850568 995598
 0 200 2465 0 1175850568 998079
 0 201 2513 0 1175850569 608
 0 202 2038 0 1175850569 2663</PRE
><P>
  </P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN134283"
>F.26.8. Per-Statement Latencies</A
></H2
><P
>   With the <TT
CLASS="LITERAL"
>-r</TT
> option, <SPAN
CLASS="APPLICATION"
>pgbench</SPAN
> collects
   the elapsed transaction time of each statement executed by every
   client.  It then reports an average of those values, referred to
   as the latency for each statement, after the benchmark has finished.
  </P
><P
>   For the default script, the output will look similar to this:
</P><PRE
CLASS="SCREEN"
>starting vacuum...end.
transaction type: TPC-B (sort of)
scaling factor: 1
query mode: simple
number of clients: 10
number of threads: 1
number of transactions per client: 1000
number of transactions actually processed: 10000/10000
tps = 618.764555 (including connections establishing)
tps = 622.977698 (excluding connections establishing)
statement latencies in milliseconds:
        0.004386        \set nbranches 1 * :scale
        0.001343        \set ntellers 10 * :scale
        0.001212        \set naccounts 100000 * :scale
        0.001310        \setrandom aid 1 :naccounts
        0.001073        \setrandom bid 1 :nbranches
        0.001005        \setrandom tid 1 :ntellers
        0.001078        \setrandom delta -5000 5000
        0.326152        BEGIN;
        0.603376        UPDATE pgbench_accounts SET abalance = abalance + :delta WHERE aid = :aid;
        0.454643        SELECT abalance FROM pgbench_accounts WHERE aid = :aid;
        5.528491        UPDATE pgbench_tellers SET tbalance = tbalance + :delta WHERE tid = :tid;
        7.335435        UPDATE pgbench_branches SET bbalance = bbalance + :delta WHERE bid = :bid;
        0.371851        INSERT INTO pgbench_history (tid, bid, aid, delta, mtime) VALUES (:tid, :bid, :aid, :delta, CURRENT_TIMESTAMP);
        1.212976        END;</PRE
><P>
  </P
><P
>   If multiple script files are specified, the averages are reported
   separately for each script file.
  </P
><P
>   Note that collecting the additional timing information needed for
   per-statement latency computation adds some overhead.  This will slow
   average execution speed and lower the computed TPS.  The amount
   of slowdown varies significantly depending on platform and hardware.
   Comparing average TPS values with and without latency reporting enabled
   is a good way to measure if the timing overhead is significant.
  </P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="AEN134292"
>F.26.9. Good Practices</A
></H2
><P
>   It is very easy to use <SPAN
CLASS="APPLICATION"
>pgbench</SPAN
> to produce completely
   meaningless numbers.  Here are some guidelines to help you get useful
   results.
  </P
><P
>   In the first place, <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>never</I
></SPAN
> believe any test that runs
   for only a few seconds.  Use the <TT
CLASS="LITERAL"
>-t</TT
> or <TT
CLASS="LITERAL"
>-T</TT
> option
   to make the run last at least a few minutes, so as to average out noise.
   In some cases you could need hours to get numbers that are reproducible.
   It's a good idea to try the test run a few times, to find out if your
   numbers are reproducible or not.
  </P
><P
>   For the default TPC-B-like test scenario, the initialization scale factor
   (<TT
CLASS="LITERAL"
>-s</TT
>) should be at least as large as the largest number of
   clients you intend to test (<TT
CLASS="LITERAL"
>-c</TT
>); else you'll mostly be
   measuring update contention.  There are only <TT
CLASS="LITERAL"
>-s</TT
> rows in
   the <TT
CLASS="STRUCTNAME"
>pgbench_branches</TT
> table, and every transaction wants to
   update one of them, so <TT
CLASS="LITERAL"
>-c</TT
> values in excess of <TT
CLASS="LITERAL"
>-s</TT
>
   will undoubtedly result in lots of transactions blocked waiting for
   other transactions.
  </P
><P
>   The default test scenario is also quite sensitive to how long it's been
   since the tables were initialized: accumulation of dead rows and dead space
   in the tables changes the results.  To understand the results you must keep
   track of the total number of updates and when vacuuming happens.  If
   autovacuum is enabled it can result in unpredictable changes in measured
   performance.
  </P
><P
>   A limitation of <SPAN
CLASS="APPLICATION"
>pgbench</SPAN
> is that it can itself become
   the bottleneck when trying to test a large number of client sessions.
   This can be alleviated by running <SPAN
CLASS="APPLICATION"
>pgbench</SPAN
> on a different
   machine from the database server, although low network latency will be
   essential.  It might even be useful to run several <SPAN
CLASS="APPLICATION"
>pgbench</SPAN
>
   instances concurrently, on several client machines, against the same
   database server.
  </P
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="pgarchivecleanup.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="pgbuffercache.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>pg_archivecleanup</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="contrib.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>pg_buffercache</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>